package com.lrkj.back.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.entity.ZsEquip;
import com.lrkj.back.entity.ZsEquipCeshi;
import com.lrkj.back.entity.ZsSoft;
import com.lrkj.back.service.ZsEquipService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

//import cn.com.cbons.mamisay.openapi.entity.TbOperationPackage;
import com.lrkj.back.service.TbEmployeeService;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsEquipCeshiService;
import com.lrkj.back.service.ZsMiejunService;
import com.lrkj.back.service.ZsPackageImformationService;
import com.lrkj.back.service.ZsSoftService;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/equip")
public class ZsEquipController extends BaseController{
	@Autowired
	private ZsEquipService zsEquipService;
	@Autowired
	private TbSeqService seqService;
	@Autowired
	private ZsSoftService softService;
	@Autowired
	private ZsEquipCeshiService zsEquipCeshiService;
	@Autowired
	private TbEmployeeService employeeService;
	@Autowired
	private ZsMiejunService miejunService;
	@Autowired
	private ZsPackageImformationService zsPackageInformationService;

	//配置--设备管理  （点击查询）
	@RequestMapping(value="/equipList.do")
	public ModelAndView equipList(String searchTxt,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<String,Object>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("equipName", searchTxt.trim());
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsEquip> page = zsEquipService.findAllZsEquipList(map);
		request.setAttribute("equipList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/peizhi/shebei/equip_list");
	}
		
	@RequestMapping("/equipEdit.do")
	public ModelAndView equipEdit(Integer  equipId, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsEquip zsEquip = (equipId == null) ? new ZsEquip() : zsEquipService.selectByPrimaryKey(equipId);
		if(equipId == null){
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
			String equipNo = sdf.format(new Date())+seqService.seqVal("equipno");
			zsEquip.setEquipNo(equipNo);
		}
		request.setAttribute("zsEquip", zsEquip);
		return new ModelAndView("/peizhi/shebei/equip_edit");
	}
	@RequestMapping(value="/equipSave.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<Object,Object> equipSave(ZsEquip zsEquip ,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			zsEquip.setEquipCreatetime(new Date());
			if(zsEquip.getEquipId() == null ){
				zsEquipService.insert(zsEquip);
			}else{
				zsEquipService.updateByPrimaryKey(zsEquip);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping(value="/equipDelete.do")
	@ResponseBody
	public Map equipDelete(HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String equipId = request.getParameter("equipId");
			String[] equipIds = equipId.split(",");
			for(String equId : equipIds){
				zsEquipService.deleteByPrimaryKey(equId);
				//packageproductService.deleteByPackageId(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	//灭菌--BD测试   设备表 和 设备测试表
	@RequestMapping("bdCeshi.do")
	public ModelAndView bdCeshi(HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> map1 = new HashMap<>();
		Map<String, String> equipSoft = new HashMap<>();
		Date date = new Date();
		map.put("equipType", 2);
		map.put("equipCategory",181);
		List<ZsEquip> equips = zsEquipService.findAllZsEquipListNoPage(map);
		List<Integer> nums = new ArrayList<>();
		nums.add(0);
		nums.add(1);
		nums.add(2);
		List<Integer []> equipList = new ArrayList<>();
		Integer [] equipState = null;
	    ZsSoft soft = null;
	    ZsEquipCeshi equipCeshi = null;
		for (ZsEquip zsEquip : equips) {
			map.put("equipId", zsEquip.getEquipId());
			map.put("softType", 3);
			soft = softService.findOne(map);
			equipSoft.put(zsEquip.getEquipId().toString(), soft.getSoftId().toString());
			map1.put("ceshiEquipId", zsEquip.getEquipId());
			map1.put("ceshiDate", date);
			map1.put("arr", nums);
			/*map.put("ceshiEquipId", zsEquip.getEquipId());
			map.put("ceshiDate", date);
			map.put("arr", nums);*/
			//0 可进行测试((第一天合格第二天可查出)可点击开始)  1 测试进行中(可点击暂停和结束) 2 测试结束(可点击审核) 3 啥都不能点 

			equipCeshi = zsEquipCeshiService.findOne(map1);
			equipState = new Integer[2];
			if (equipCeshi != null) {
				if (equipCeshi.getCeshiHege() == 2 && equipCeshi.getCeshiState() == 1) {
					equipState[0] = zsEquip.getEquipId();
					equipState[1] = 0;
					equipList.add(equipState);
				}
				if (equipCeshi.getCeshiHege() == 0 && equipCeshi.getCeshiState() == 0) {
					equipState[0] = zsEquip.getEquipId();
					equipState[1] = 1;
					equipList.add(equipState);
				}
				if (equipCeshi.getCeshiHege() == 0 && equipCeshi.getCeshiState() == 1) {
					equipState[0] = zsEquip.getEquipId();
					equipState[1] = 2;
					equipList.add(equipState);
				}
				if (equipCeshi.getCeshiHege() == 1) {
					equipState[0] = zsEquip.getEquipId();
					equipState[1] = 3;
					equipList.add(equipState);
				}
			}else {
				equipState[0] = zsEquip.getEquipId();
				equipState[1] = 0;
				equipList.add(equipState);
			}
		}

		request.setAttribute("equipSoft", JSON.toJSON(equipSoft));
		request.setAttribute("softMap", JSON.toJSON(StartUpOnLoadData.softMap));
		request.setAttribute("equipList", JSON.toJSON(equipList));
		request.setAttribute("lstEquips", equips);
		return new ModelAndView("/mj/bdceshi/bd_ceshi_equip");
	}

	//灭菌测试开始的方法（点击开始按钮）
	@RequestMapping("startCeshi.do")
	@ResponseBody
	public Map startCeshi(HttpServletRequest request, String ceshiId, String softId, HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		try {
    		Date date = new Date();
    		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
//    		Map<String, Object> ormap = new HashMap<String,Object>();
//    		ormap.put("ceshiEquipId", ceshiId);
//  		ormap.put("ceshiDate", date);
//  		ormap.put("ceshiHege", 2);
//          List<ZsEquipCeshi> youornot = zsEquipCeshiService.findByDateHegeState(ormap);
//          ZsEquipCeshi ceshiequip = new ZsEquipCeshi();
            TbEmployee tbEmployee = (TbEmployee)session.getAttribute("emp");
            if (tbEmployee!=null) {
            	Map<String, Object> map = new HashMap<String,Object>();
            	Map<String, Object> map1 = new HashMap<String,Object>();
        		
            	map.put("ceshiEquipId", ceshiId);
        		map.put("ceshiDate", date);
        		map.put("ceshiHege", 1);
        		map.put("ceshiState", 1);
        		map1.put("ceshiEquipId", ceshiId);
        		map1.put("ceshiDate", date);
        		map1.put("ceshiState", 0);
//        		Integer j=0;
        		List<ZsEquipCeshi> list1 = zsEquipCeshiService.findByDateHegeState(map1);
        		List<ZsEquipCeshi> list = zsEquipCeshiService.findByDateHegeState(map);
        		if (list.isEmpty() && list1.isEmpty()) {
        			/*if (youornot.size()==1) {
        					//修改该测试流水的信息
        					Map<String, Object> updateMap = new HashMap<String,Object>();
        					updateMap.put("ceshiLiushui", sdf.format(date)+seqService.seqVal("ceshino"));
        					updateMap.put("ceshiEquipId", ceshiId);
        					updateMap.put("ceshiSoft", softId);
        					updateMap.put("ceshirenTiaoma", tbEmployee.getEmployeeNo());
        					updateMap.put("ceshiKaishiTime", date);
        					updateMap.put("ceshiJieshuTime", null);
        					updateMap.put("ceshiDate", date);
        					updateMap.put("ceshiHege", 2);
        					updateMap.put("ceshiHege1", 0);
        					updateMap.put("ceshiState", 0);
        					zsEquipCeshiService.updateCeshi(updateMap);
        					j=1;
        			}*/
//        			if (j!=1) {
        				ZsEquipCeshi equipCeshi = new ZsEquipCeshi();
        				equipCeshi.setCeshiDate(date);
        				equipCeshi.setCeshiEquipId(Integer.valueOf(ceshiId));
        				equipCeshi.setCeshiHege(0);
        				equipCeshi.setCeshiState(0);
        				equipCeshi.setCeshiKaishiTime(date);
        				equipCeshi.setCeshirenTiaoma(tbEmployee.getEmployeeNo());
         				equipCeshi.setCeshiSoft(softId);
        				equipCeshi.setCeshiLiushui(sdf.format(date)+seqService.seqVal("ceshino"));
                    	zsEquipCeshiService.insert(equipCeshi);
//					}
        			
                	rtnMap.put("state", "true");
                	rtnMap.put("msg", "保存成功");
    			}
        		else {
        			rtnMap.put("state", "false");
                	rtnMap.put("msg", "该设备今天已进行过测试或正在测试中");
    			}
            }else {
				rtnMap.put("state", "false");
            	rtnMap.put("msg", "无效审核人条码");
			}
		} catch (Exception e) {
			rtnMap.put("state", "false");
        	rtnMap.put("msg", "测试失败");
        	e.printStackTrace();
		}
		return rtnMap;
	}

	//灭菌测试结束的方法
	@RequestMapping("endCeshi.do")
	@ResponseBody
	public Map endCeshi(String ceshiId, HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Map<String, Object> map = new HashMap<String,Object>();
    	Map<Object, Object> rtnMap = new HashMap<Object,Object>();
    	Date date = new Date();
    	//String[] ceshiLiushui = request.getParameter("id").split(",");
    	try {
    		map.put("ceshiJieshuTime", date);
    		map.put("ceshiState", 1);
    		map.put("ceshiState1", 0);
    		map.put("ceshiHege",0);
    		map.put("ceshiEquipId", ceshiId);
    		map.put("ceshiDate", date);
    		zsEquipCeshiService.updateEndtimeAndState(map);
			rtnMap.put("state", "true");
    		rtnMap.put("msg", "保存成功");
		} catch (Exception e) {
			rtnMap.put("state", "false");
    		rtnMap.put("msg", "失败");
    		e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("ceshiHege.do")
	@ResponseBody
	public Map ceshiHege(String ceshiId, String hege, HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Map<String, Object> map = new HashMap<String,Object>();
    	Map<Object, Object> rtnMap = new HashMap<Object,Object>();
    	Date date = new Date();
    	try {
    		map.put("ceshiState1", 1);
    		map.put("ceshiHege1",0);
    		map.put("ceshiHege",hege);
    		map.put("ceshiEquipId", ceshiId);
    		map.put("ceshiDate", date);
    		map.put("ceshiShenheTime", date);
    		zsEquipCeshiService.updateEndtimeAndState(map);
			rtnMap.put("state", "true");
    		rtnMap.put("msg", "保存成功");
		} catch (Exception e) {
			rtnMap.put("state", "false");
    		rtnMap.put("msg", "保存失败");
    		e.printStackTrace();
		}
		return rtnMap;
	}
}